<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>顶点和几何体</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      /* 隐藏body窗口区域滚动条 */
    }
  </style>
  <!--引入three.js三维引擎-->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/controls/OrbitControls.js"></script>
  <!-- <script src="./three.js"></script> -->
  <!-- <script src="http://www.yanhuangxueyuan.com/threejs/build/three.js"></script> -->
</head>

<body>
  <script>
    /**
     * 创建场景对象Scene
     */
    let scene = new THREE.Scene();
    /**
     * 创建网格模型
     */
     let vertices = new Float32Array([
        0, 0, 0,
        50, 0, 0,
        50, 50, 0,
        0, 0, 0,
        50, 50, 0,
        50, 50, 50,
     ])
    let geometry = new THREE.BufferGeometry()
    let attribue = new THREE.BufferAttribute(vertices, 3)
    geometry.attributes.position = attribue
    
    // 三角面(网格)渲染模式
    let material = new THREE.MeshBasicMaterial({
      color: 0x004488, //三角面颜色
      side: THREE.DoubleSide //两面可见
    }); //材质对象
    let meshes = []
    let mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
    scene.add(mesh); //网格模型添加到场景中
    meshes.push(mesh)
    
    // 点渲染模式
    pointMaterial = new THREE.PointsMaterial({
        color: 0x88ff88,
        size: 10.0 //点对象像素尺寸
    }); //材质对象
    mesh = new THREE.Points(geometry, pointMaterial); //网格模型对象Mesh
    scene.add(mesh); //网格模型添加到场景中
    meshes.push(mesh)
    
    // 线条渲染模式
    lineMaterial = new THREE.LineBasicMaterial({
        color:0x44ffff //线条颜色
    });//材质对象
    let line=new THREE.Line(geometry,lineMaterial);//线条模型对象
    scene.add(line);//线条对象添加到场景中
    meshes.push(mesh)
    
    var colors = new Float32Array([
      1, 0, 0, //顶点1颜色
      0, 1, 0, //顶点2颜色
      0, 0, 1, //顶点3颜色

      1, 1, 0, //顶点4颜色
      0, 1, 1, //顶点5颜色
      1, 0, 1, //顶点6颜色
    ]);
    geometry.attributes.color = new THREE.BufferAttribute(colors, 3)
    
    // 点渲染模式
    let pointMaterial1 = new THREE.PointsMaterial({
        // 使用顶点颜色渲染模型
        vertexColors: THREE.VertexColors,
        size: 10.0 //点对象像素尺寸
    }); //材质对象
    mesh = new THREE.Points(geometry, pointMaterial1)
    mesh.position.set(0, 0, 50)
    scene.add(mesh)
    meshes.push(mesh)
    
    // 线条渲染模式
    let lineMaterial1 = new THREE.LineBasicMaterial({
        // 使用顶点颜色渲染模型
        vertexColors: THREE.VertexColors
    });//材质对象
    line=new THREE.Line(geometry,lineMaterial1);//线条模型对象
    line.position.set(0, 0, 50)
    scene.add(line);//线条对象添加到场景中
    meshes.push(line)
    
    let material1 = new THREE.MeshBasicMaterial({
        // 使用顶点颜色渲染模型
        vertexColors: THREE.VertexColors,
        side: THREE.DoubleSide //两面可见
    }); //材质对象
    mesh = new THREE.Mesh(geometry, material1); //网格模型对象Mesh
    mesh.position.set(0, 0, 50)
    scene.add(mesh); //网格模型添加到场景中
    meshes.push(mesh)
    
    
    let material2 = new THREE.MeshBasicMaterial({
        color: 0x885511,
        side: THREE.DoubleSide //两面可见
    }); //材质对象
    let normals = new Float32Array([
        0, -1, 0, //顶点1法向量
        0, -1, 0, //顶点2法向量
        0, -1, 0, //顶点3法向量

        -1, 1, 0, //顶点4法向量
        -1, 1, 0, //顶点5法向量
        -1, 1, 0, //顶点6法向量
    ]);
    geometry.attributes.normal = new THREE.BufferAttribute(normals, 3)
    mesh = new THREE.Mesh(geometry, material2); //网格模型对象Mesh
    mesh.position.set(50, 0, 50)
    scene.add(mesh); //网格模型添加到场景中
    meshes.push(mesh)
    
    let geometry_index = new THREE.BufferGeometry()
    vertices = new Float32Array([
         0,  0,  0, // 顶点1坐标
        50,  0,  0, // 顶点2坐标
        50, 50,  0, // 顶点3坐标
         0, 50,  0  // 顶点4坐标
    ])
    
    geometry_index.attributes.position = new THREE.BufferAttribute(vertices, 3)
    
    normals = new Float32Array([
        0, 0, 1,
        0, 0, 1,
        0, 0, 1,
        0, 0, 1,
    ])
    geometry_index.attributes.normal = new THREE.BufferAttribute(normals, 3)
    
    let colors_index = new Float32Array([
        1, 0, 0,
        0, 1, 0,
        0, 0, 1,
        1, 0, 0,
    ])
    geometry_index.attributes.color = new THREE.BufferAttribute(colors_index, 3)
    
    let index = new Uint16Array([
        0, 1, 2,
        0, 2, 3
    ])
    geometry_index.index = new THREE.BufferAttribute(index, 1)
    mesh = new THREE.Mesh(geometry_index, material1); //网格模型对象Mesh
    mesh.position.set(0, 50, 50)
    scene.add(mesh); //网格模型添加到场景中
    meshes.push(mesh)
    
    
    let geometry1 = new THREE.Geometry()
    geometry1.vertices.push(
        new THREE.Vector3(0, 0, 0),
        new THREE.Vector3(50, 0, 0),
        new THREE.Vector3(50, 50, 0),
        new THREE.Vector3(0, 50, 0)
    )
    geometry1.colors.push(
        new THREE.Color(0x884444),
        new THREE.Color(0x448844),
        new THREE.Color(0x444488),
        new THREE.Color(0x884444)
    )
    let face1 = new THREE.Face3(0, 1, 2)
    face1.color = new THREE.Color(0x884444)
    face1.vertexColors = [
        new THREE.Color(0x884444),
        new THREE.Color(0x448844),
        new THREE.Color(0x444488)
    ]
    face1.normal = new THREE.Vector3(0, 0, 1)
    face1.vertexNormals.push(
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(0, 0, 1)
    )
    let face2 = new THREE.Face3(0, 2, 3)
    face2.color = new THREE.Color(0x448844)
    face2.vertexColors = [
        new THREE.Color(0x448844),
        new THREE.Color(0x444488),
        new THREE.Color(0x884444)
    ]
    face2.normal = new THREE.Vector3(0, 0, 1)
    face2.vertexNormals.push(
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(0, 0, 1)
    )
    geometry1.faces.push(face1, face2)
    mesh = new THREE.Mesh(geometry1, material1); //网格模型对象Mesh
    mesh.position.set(50, 50, 50)
    scene.add(mesh); //网格模型添加到场景中
    
    
    let axisHelper = new THREE.AxesHelper(250);
    scene.add(axisHelper);
    /**
     * 光源设置
     */
    //点光源
    let point = new THREE.PointLight(0xffff00);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    //环境光
    let ambient = new THREE.AmbientLight(0x444444);
    scene.add(ambient);
    // console.log(scene)
    // console.log(scene.children)
    /**
     * 相机设置
     */
    let width = window.innerWidth; //窗口宽度
    let height = window.innerHeight; //窗口高度
    let k = width / height; //窗口宽高比
    let s = 200; //三维场景显示范围控制系数，系数越大，显示的范围越大
    //创建相机对象
    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(0, 300, 200); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    /**
     * 创建渲染器对象
     */
    let renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);//设置渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
    
    Game = {
		tickLength: 16, 				// 20Hz
		lastTick: performance.now(),	// 最后更新时间
		stopKey: 0,
		canvas: null,
		context: null,
	}
    function render(tFrame) {
        if (tFrame === undefined) {
            tFrame = performance.now()
            Game.lastTick = performance.now()
        }
        let nextTick = Game.lastTick + Game.tickLength
        let tickNum = 0
        if (tFrame > nextTick) {
            tickNum = Math.floor((tFrame - Game.lastTick) / Game.tickLength)
        }
        Game.lastTick = tFrame
        /*
        for (let num = 0; num < tickNum; ++num) {
            for (let i = 0, l = meshes.length; i < l; ++i) {
                meshes[i].position.set((i + 0.5 - l / 2) * 150, 0, 0)
                meshes[i].rotateY(3.14 / 180)
            }
        }
        */
        //执行渲染操作   指定场景、相机作为参数
        renderer.render(scene, camera);
        Game.stopKey = requestAnimationFrame(render)
    }
    render()
    let controls = new THREE.OrbitControls(camera,renderer.domElement);//创建控件对象
    controls.addEventListener('change', render);
  </script>
</body>
</html>
